﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MisClient.Common;

namespace MisClient
{
    public partial class FrmVolumeBusiness : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.IFkType typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteFkType();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();
        private IBLL.ICusDep cusDepBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCusDep();
        private IBLL.IShop shopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteShop();
        private IBLL.ICusShop cusShopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCusShop();

        public FrmVolumeBusiness()
        {
            InitializeComponent();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmVolumeBusiness_Load(object sender, EventArgs e)
        {
            this.openEndDate.Format = DateTimePickerFormat.Custom;
            this.openEndDate.CustomFormat = " ";
            this.expireStarDate.Format = DateTimePickerFormat.Custom;
            this.expireStarDate.CustomFormat = " ";
            this.expireEndDate.Format = DateTimePickerFormat.Custom;
            this.expireEndDate.CustomFormat = " ";
            this.txtDepCode.Text = accountUser.DeptCode;
            this.openStarDate.Value = DateTime.Now.Date.AddDays(-7);
            this.openStarDate.Focus();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            if (this.txtDepCode.Text != "1")
            {
                sb.Append(" and bf.dep_id=@depId");
                dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            }
            sb.Append(" and bf.date>=@startDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            if (!string.IsNullOrEmpty(this.openEndDate.Text.ToString().Trim()))
            {
                sb.Append(" and bf.date<=@endDate");
                DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
                time = time.Date.AddDays(1);
                dictionary.Add("@endDate", time.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.expireStarDate.Text.ToString().Trim()))
            {
                sb.Append(" and bf.d_kp>=@gqStartDate");
                dictionary.Add("@gqStartDate", this.expireStarDate.Value.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.expireEndDate.Text.ToString().Trim()))
            {
                sb.Append(" and bf.d_kp<=@gqEndDate");
                DateTime time = DateTime.Parse(this.expireEndDate.Value.ToShortDateString());
                time = time.Date.AddDays(1);
                dictionary.Add("@gqEndDate", time.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                sb.Append(" and bf.no=@no");
                dictionary.Add("@no", this.txtNo.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
            {
                sb.Append(" and bf.type_js=@typeJs");
                dictionary.Add("@typeJs", this.txtFKID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" and bf.id=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
            {
                sb.Append(" and bf.shop_id=@shopId");
                dictionary.Add("@shopId", this.txtShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
            {
                sb.Append(" and bf.cus_id=@cusId");
                dictionary.Add("@cusId", this.txtCusCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
            {
                sb.Append(" and bf.cus_dep=@cusDep");
                dictionary.Add("@cusDep", this.txtCusDepCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
            {
                sb.Append(" and bf.cus_shop=@cusShop");
                dictionary.Add("@cusShop", this.txtCusShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
            {
                sb.Append(" and bf.type1=@type1");
                dictionary.Add("@type1", this.txtType1.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
            {
                sb.Append(" and bf.type2=@type2");
                dictionary.Add("@type2", this.txtType2.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
            {
                sb.Append(" and bf.type3=@type3");
                dictionary.Add("@type3", this.txtType3.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtTh.Text.Trim()))
            {
                sb.Append(" and bf.f2=@f2");
                dictionary.Add("@f2", this.txtTh.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtKp.Text.Trim()))
            {
                sb.Append(" and bf.f3=@f3");
                dictionary.Add("@f3", this.txtKp.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtSk.Text.Trim()))
            {
                sb.Append(" and bf.f4=@f4");
                dictionary.Add("@f4", this.txtSk.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtInvoiceNum.Text.Trim()))
            {
                sb.Append(" and bf.no_cus=@noCus");
                dictionary.Add("@noCus", this.txtInvoiceNum.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.textBox7.Text.Trim()))
            {
                sb.Append(" and bf.oper=@oper1");
                dictionary.Add("@oper1", this.textBox7.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtBei.Text.Trim()))
            {
                sb.Append(" and bf.bei=@bei");
                dictionary.Add("@bei", this.txtBei.Text.Trim());
            }
            DataTable dt = sacfBLL.GetVolumeAll(sb.ToString(), dictionary);
            decimal count = 0;
            decimal hsPrice = 0;
            decimal cbPrice = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    String[] DataRow = new String[30];
                    DataRow[0] = dt.Rows[i]["dep_id"].ToString();
                    DataRow[1] = dt.Rows[i]["no"].ToString();
                    DataRow[2] = dt.Rows[i]["shop_id"].ToString();
                    DataRow[3] = string.Format("{0} {1}", dt.Rows[i]["id"].ToString(), dt.Rows[i]["goods"].ToString());
                    DataRow[4] = dt.Rows[i]["sl_p"].ToString();
                    DataRow[5] = dt.Rows[i]["sl"].ToString();
                    count += decimal.Parse(DataRow[5]);
                    DataRow[6] = dt.Rows[i]["je_hs"].ToString();
                    hsPrice += decimal.Parse(DataRow[6]);
                    DataRow[7] = dt.Rows[i]["f1"].ToString();
                    DataRow[8] = dt.Rows[i]["f2"].ToString();
                    DataRow[9] = dt.Rows[i]["f3"].ToString();
                    DataRow[10] = dt.Rows[i]["f4"].ToString();
                    DataRow[11] = dt.Rows[i]["f8"].ToString();
                    DataRow[12] = dt.Rows[i]["date"].ToString();
                    DataRow[13] = dt.Rows[i]["d_th"].ToString();
                    DataRow[14] = dt.Rows[i]["d_kp"].ToString();
                    DataRow[15] = dt.Rows[i]["d_js"].ToString();
                    DataRow[16] = dt.Rows[i]["d_ok"].ToString();
                    DataRow[17] = dt.Rows[i]["date_js"].ToString();
                    DataRow[18] = dt.Rows[i]["type_js"].ToString();
                    DataRow[19] = dt.Rows[i]["day_js"].ToString();
                    DataRow[20] = string.Format("{0}-{1}-{2}", dt.Rows[i]["cus_id"].ToString(), dt.Rows[i]["cus_dep"].ToString(), dt.Rows[i]["cus_shop"].ToString());
                    DataRow[21] = dt.Rows[i]["no_cus"].ToString();
                    DataRow[22] = dt.Rows[i]["price_l"].ToString();
                    DataRow[23] = dt.Rows[i]["pricei"].ToString();
                    cbPrice += decimal.Parse(DataRow[23])*count;
                    DataRow[24] = dt.Rows[i]["price_hs"].ToString();
                    DataRow[25] = dt.Rows[i]["nono"].ToString();
                    DataRow[26] = dt.Rows[i]["dm"].ToString();
                    DataRow[27] = dt.Rows[i]["unit"].ToString();
                    DataRow[28] = dt.Rows[i]["oper"].ToString();
                    DataRow[29] = dt.Rows[i]["bei"].ToString();
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.label17.Text = string.Format("●合计件数：{0} 含税金额：{1} 成本：{2}", (int)count, hsPrice, cbPrice);
            }
            else
            {
                this.label17.Text = "";
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                if (this.txtDepCode.Text.Trim().Length < 1)
                {
                    MessageBox.Show("部门代码长度为1！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.expireStarDate.Focus();
            }
        }

        private void txtCusCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
                {
                    MisModel.Customer customer = cusBLL.GetById(this.txtCusCode.Text.Trim());
                    if (customer == null)
                    {
                        MessageBox.Show("无此代码或不属于本部分！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusCode.Focus();
                        return;
                    }
                    this.txtBoxCus.Text = customer.Customer_;
                }
                this.txtCusDepCode.Focus();
            }
        }

        private void txtCusDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
                {
                    MisModel.CusDep cusDep = cusDepBLL.GetBy(this.txtCusCode.Text.Trim(), this.txtCusDepCode.Text.Trim());
                    if (cusDep == null)
                    {
                        MessageBox.Show("客户部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusDepCode.Focus();
                        return;
                    }
                    else
                    {
                        this.txtBoxCusDep.Text = cusDep.Depart;
                    }
                }
                this.txtCusShopCode.Focus();
            }
        }

        private void txtFKID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
                {
                    MisModel.FKType fkType = typeBLL.GetById(this.txtFKID.Text.Trim());
                    if (fkType == null)
                    {
                        MessageBox.Show("无此代码或不属于此结算类型！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtFKID.Focus();
                        return;
                    }
                    else
                    {
                        this.txtBoxFK.Text = fkType.FkType;
                    }
                }
                this.textBox7.Focus();
            }
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
                {
                    MisEntity.Shop shop=shopBLL.GetId(this.txtShopCode.Text.Trim());
                    if (shop == null)
                    {
                        MessageBox.Show("提货仓代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtShopCode.Focus();
                        return;
                    }
                    else
                    {
                        this.txtBoxShop.Text = shop.SHOP;
                    }
                }
                this.txtType1.Focus();
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (goods == null)
                    {
                        MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                    this.txtGoods.Text = goods.GOODS;
                    this.txtDM.Text = goods.DM;
                    this.txtID.Text = goods.ID;
                }
                this.txtShopCode.Focus();
            }
        }

        private void txtCusShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
                {
                    MisModel.CusShop cusShop = cusShopBLL.GetBy(this.txtCusCode.Text.Trim(), this.txtCusShopCode.Text.Trim());
                    if (cusShop == null)
                    {
                        MessageBox.Show("客户分店不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusShopCode.Focus();
                        return;
                    }
                    else
                    {
                        this.txtBoxCusShop.Text = cusShop.CusShop_;
                    }
                }
                this.txtNo.Focus();
            }
        }

        private void openStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.openEndDate.Focus();
            }
        }

        private void openEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
            
        }

        private void expireStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.expireEndDate.Focus();
            }
            
        }

        private void expireEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtCusCode.Focus();
            }
            
        }

        private void txtNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtInvoiceNum.Focus();
            }
            
        }

        private void txtInvoiceNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtFKID.Focus();
            }
            
        }

        private void textBox7_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBox8.Focus();
            }
            
        }

        private void textBox8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtTh.Focus();
            }
            
        }

        private void txtTh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtKp.Focus();
            }
            
        }

        private void txtKp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtSk.Focus();
            }
            
        }

        private void txtSk_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtBei.Focus();
            }
            
        }

        private void txtBei_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtID.Focus();
            }
            
        }

        private void txtType1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtType2.Focus();
            }
            
        }

        private void txtType2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtType3.Focus();
            }
            
        }

        private void txtType3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnOk.Focus();
            }
            
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void openEndDate_ValueChanged(object sender, EventArgs e)
        {
            this.openEndDate.Format = DateTimePickerFormat.Long;
            this.openEndDate.CustomFormat = null;
        }

        private void expireStarDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireStarDate.Format = DateTimePickerFormat.Long;
            this.expireStarDate.CustomFormat = null;
            
        }

        private void expireEndDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireEndDate.Format = DateTimePickerFormat.Long;
            this.expireEndDate.CustomFormat = null;
            
        }

        private void btnZoom_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Location.Y == 10)
            {
                this.dataGridView1.Location = new Point(this.dataGridView1.Location.X, 250);
                this.dataGridView1.Size = new Size(this.dataGridView1.Width, this.Height - 380);
            }
            else
            {
                this.dataGridView1.Location = new Point(this.dataGridView1.Location.X, 10);
                this.dataGridView1.Size = new Size(this.dataGridView1.Width, this.Height - 140);
            }
           
        }

        private void btnPri_Click(object sender, EventArgs e)
        {
            PrintDGV.Print_DataGridView(this.dataGridView1);
        }
    }
}
